package thread;

public class demo22 {

    public static void main(String[] args) throws InterruptedException {
        //循环打印abc
        Object locker1=new Object();
        Object locker2=new Object();
        Object locker3=new Object();
        Thread t1=new Thread(()->{
            for (int i = 0; i < 10; i++) {
                synchronized (locker1){
                    try {
                        locker1.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println("a");
                synchronized (locker2){
                    locker2.notify();
                }
            }
            //打印好a后唤醒t2线程去打印b

        });
        Thread t2=new Thread(()->{
            for (int i = 0; i < 10; i++) {
                synchronized (locker2){
                    try {
                        locker2.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println("b");
                //打印完b后唤醒t3线程去打印c
                synchronized (locker3){
                    locker3.notify();
                }
            }
        });
        Thread t3=new Thread(()->{
            for (int i = 0; i < 10; i++) {
                synchronized (locker3){
                    try {
                        locker3.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println("c");
                //唤醒t1线程打印a
                synchronized (locker1){
                    locker1.notify();
                }

            }
        });
        t1.start();
        t2.start();
        t3.start();
        //手动唤醒t1线程先打印a
        Thread.sleep(1000);
        synchronized (locker1){
            locker1.notify();
        }

    }

}
